package org.example.gxpt.service.impl;

import org.example.gxpt.common.result.Result;
import org.example.gxpt.mapper.ShowroomMapper;
import org.example.gxpt.pojo.dto.ShowroomDTO;
import org.example.gxpt.pojo.dto.ShowroomUpdateDTO;
import org.example.gxpt.pojo.entity.Showroom;
import org.example.gxpt.pojo.entity.ShowroomAdmin;
import org.example.gxpt.pojo.userdetails.LoginUser;
import org.example.gxpt.service.ShowroomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShowroomServiceImpl implements ShowroomService {

    @Autowired
    private ShowroomMapper showroomMapper;

    /**
     * 保存展厅
     *
     * @param showroomDTO
     * @return
     */
    @Override
    public Result saveShowroom(ShowroomDTO showroomDTO) {

        Showroom showroom = new Showroom();
        BeanUtils.copyProperties(showroomDTO, showroom);
        showroomMapper.save(showroom);
        return Result.success();
    }

    /**
     * 根据当前上下文更新展厅(展厅管理员使用)
     *
     * @param showroomUpdateDTO
     * @return
     */
    @Override
    public Result updateByContext(ShowroomUpdateDTO showroomUpdateDTO) {

        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        ShowroomAdmin showroomAdmin = loginUser.getShowroomAdmin();

        if(showroomAdmin == null) return Result.error("当前不为展厅管理员，无法操作");

        Long showroomId = showroomAdmin.getShowroomId();
        Showroom showroom = new Showroom();
        BeanUtils.copyProperties(showroomUpdateDTO, showroom);
        showroom.setId(showroomId);

        showroomMapper.update(showroom);

        return Result.success();
    }

    /**
     * 根据id更新展厅(上一级管理员使用)
     *
     * @param showroomUpdateDTO
     * @return
     */
    @Override
    public Result updateById(ShowroomUpdateDTO showroomUpdateDTO) {

            Showroom showroom = new Showroom();
            BeanUtils.copyProperties(showroomUpdateDTO, showroom);
            showroomMapper.update(showroom);

            return Result.success();
    }

    /**
     * 根据id查询展厅
     *
     * @param id
     * @return
     */
    @Override
    public Result<Showroom> getShowroomById(Long id) {

            Showroom showroom = showroomMapper.getShowroomById(id);
            return Result.success(showroom);
    }

    /**
     * 查询所有展厅
     *
     * @return
     */
    @Override
    public Result<List<Showroom>> listAll() {
        List<Showroom> showrooms = showroomMapper.listAll();
        return Result.success(showrooms);
    }

    /**
     * 删除展厅
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteShowroom(Long id) {
        showroomMapper.delete(id);
        return Result.success();
    }
}
